﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.OpenGL;
using Silvermoon.Windows;
using System.Drawing;
using Silvermoon.Images;
using Passwords.Utils;
using Silvermoon.Shapes;

namespace Passwords.Controls
{
    public class IndexItem : ContainerItem
    {
        #region statics

        private static Stack<IndexItem> items = new Stack<IndexItem>();

        private bool fromStack;

        public static void DisposeItems()
        {
            foreach (var item in items)
            {
                item.Dispose();
            }
            items.Clear();
        }

        public static IndexItem GetItem()
        {
            if (items.Count > 0)
            {
                return items.Pop();
            }
            else
            {
                return new IndexItem { fromStack = true };
            }
        }

        protected static void ReleaseItem(IndexItem item)
        {
            items.Push(item);
        }

        #endregion

        private Label l1, l2;
        private int index;

        public IndexItem()
            : base()
        {
            AutoHeight = false;
            Height = 80;
            Panel p = new Panel { Margin = Margin.Zero };
            l1 = new Label { Size = new Size(72, 72), Margin = new Margin() { VerticalCentered = true, Left = 4 }, Alignment = Alignment.Center, LineAlignment = Alignment.Center, Chrome = SysImages.Circle, Font = FontCache.IndexFont, Opacity = 240 };
            l2 = new Label { Margin = new Margin(4 + 80, 4, 20, 4), Alignment = Alignment.Near, LineAlignment = Alignment.Center, Font = FontCache.BigRegularFont };
            p.Controls.Add(l1, l2);
            Control = p;
            TextColor = PhoneColors.Light;
        }

        public FontSprite Font
        {
            get { return l2.Font; }
            set { l2.Font = value; }
        }

        public int Index
        {
            get { return index; }
            set
            {
                index = value;
                l1.Text = index.ToString();
            }
        }

        public string Name
        {
            get { return l2.Text; }
            set { l2.Text = value; }
        }

        protected override void Release()
        {
            if (fromStack) items.Push(this);
            base.Release();
        }

    }
}
